<template>
	<view class="bt-detail">
		<view class="bt-items" v-for="val in list">
			<view class="names">{{val.name}}</view>
			<view class="urls">{{val.url}}</view>
			<view class="btn-centers">
				<view class="btn" @click="copys(val)">复制链接</view>
				<view class="btns" @click="dels(val)">删除链接</view>
			</view>
		</view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				title: 'Hello',
				text:'',
				name:'',
				url:'',
				list:[]
			 }
		 },
		 onLoad(options) {
			 let self=this
			 if(options.text){
				 self.name=options.text
				 self.opendata()
				 
			 }
			 /**
		 	var db = openDatabase('mydb', '1.0', 'myDB', 1024 * 1024 * 1024);
		 	db.transaction(function (tx) {
		 	   tx.executeSql("SELECT * FROM mybts WHERE name like '%"+self.name+"%'", [], function (tx, results) {
		 	      var len = results.rows.length, i;
		 	      msg = "<p>查询记录条数: " + len + "</p>";
				  console.log(msg)
		 	      for (i = 0; i < len; i++){
		 	         self.list.push(results.rows.item(i))
		 	      }
		 	   }, null);
		 	})
			**/
			
			
		 },
		 destroyed () {
		 	plus.sqlite.closeDatabase({
		 			name: 'first',
		 			success: function(e){
		 				console.log('closeDatabase success!');
		 			},
		 			fail: function(e){
		 				console.log('closeDatabase failed: '+JSON.stringify(e));
		 			}
		 		});	
		 },
		 methods:{
			 searchdata(){
				 let self=this
				 plus.sqlite.selectSql({
						name: 'first',
						sql: "select * from database where name like '%"+self.name+"%'",
						success: function(data){
							console.log('selectSql success: ');
							//console.log(data)
							var len = data.length, i;
							for (i = 0; i < len; i++){
							   self.list.push(data[i])
							   
							}
						},
						fail: function(e){
							console.log('selectSql failed: '+JSON.stringify(e));
						}
				 });
			 },
			 opendata(){
				 let self=this
				 plus.sqlite.openDatabase({
					name: 'first',
					path: '_doc/test.db',
					success: function(e){
						console.log('openDatabase success!');
						self.searchdata()
					},
					fail: function(e){
						console.log('openDatabase failed: '+JSON.stringify(e));
						self.searchdata()
					}
				});
			 },
			 dels(val){
				 let self=this
				 plus.sqlite.executeSql({
						name: 'first',
						sql: "delete from database where id='"+val.id+"'",//删除数据表中的某条数据  若没有where id=6则清空数据中的所有数据
						success: function(e){
							console.log('executeSql success!');
							uni.showToast({
								title: '删除成功',
								icon: 'none',
								duration: 2000
							});
						},
						fail: function(e){
							console.log('executeSql failed: '+JSON.stringify(e));
							uni.showToast({
								title: '删除失败',
								icon: 'none',
								duration: 2000
							});
						}
					});
				 /**
				 var deleteDataSQL = 'DELETE FROM ' + tableName + ' WHERE id = '+val.id;
				 db.transaction(function(ctx, result) {
					 ctx.executeSql(deleteDataSQL, [mydb], function(ctx, result) {
						 console.log("删除成功 " + tableName );
					
					 }, function(tx, error) {
						 console.error('删除失败:' + tableName +error.message);
						 uni.showToast({
						 	title: '删除失败',
						 	icon: 'none',
						 	duration: 2000
						 });
					 });
				 });**/
			 },
			 copys(val){
				 uni.setClipboardData({
				     data: val.bts,
				     success: function () {
				        uni.showToast({
				        	title: '复制成功,请打开下载工具!',
				        	icon: 'none',
				        	duration: 2000
				        });
				     }
				 });
			 }
		}, 
    }
	
</script>

<style lang="scss">
	.bt-detail{
		width: 100%;
		overflow: hidden;
		margin: 0rpx auto;
		font-size: 28rpx;
		color: #333;
		.bt-items{
			width: 90%;
			overflow: hidden;
			margin: 20rpx auto;
			background: #FFFFFF;
			border-radius: 20rpx;
			.names{
				width: 100%;
				overflow: hidden;
				margin: 10rpx auto;
				background: #FFFFFF;
			}
			.urls{
				width: 100%;
				overflow: hidden;
				margin: 10rpx auto;
				background: #FFFFFF;
			}
			.btn-centers{
				width: 100%;
				overflow: hidden;
				margin: 10rpx auto;
			}
			.btns{
				width: 48%;
				overflow: hidden;
				line-height: 66rpx;
				color: #FFFFFF;
				text-align: center;
				font-size: 28rpx;
				border-radius: 20rpx;
				float: right;
				background: #ff4b56;
			}
			.btn{
				width: 48%;
				overflow: hidden;
				line-height: 66rpx;
				color: #FFFFFF;
				text-align: center;
				font-size: 28rpx;
				border-radius: 20rpx;
				float: left;
				background: #96b97d;
			}
		}
		
	}
	
</style>
